package BNS.kingPawn;

import java.util.*;
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.rmi.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import BNS.graphs.*;
import BNS.plumbing.*;

/** Provides an assembly of status displays, including:
	average payrate (social welfare),
	game status,
	elapsed time,
	accumulate pay, and
	definition of pay rates for the game.
	*/
public class StatusPanel extends JPanel {	
	
	protected int timeLimit, progressLimit;
	/** the gadgets where overall game progress and remaining play time are shown */
	protected JProgressBar progressBar, timeBar;
	/** gadget where a status message is displayed */
	protected JLabel messageBar;
	/** gadgets where winStatus is displayed */
	protected JLabel dynamicLabel, label5_0, label5_1, label5_2;
	protected PatchIcon winIcon;
//	protected final String payoffDefinition;
	public static final Color panelBackground= Color.lightGray;
	
	/** Constructs and displays a status panel.
		@param time the total length of the game, in seconds.
		@param progress the maximum achievable value of social welfare (used to scale the display)
		@param tippy a flag indicating that tips are allowed (used in displaying pay definitions)
		*/
	public StatusPanel(int time, int progress, boolean tippy) { 
		timeLimit=time; progressLimit=progress;
		GridBagLayout gridbag= new GridBagLayout();
		GridBagConstraints constraints= new GridBagConstraints();
		
		setLayout(gridbag);
		setOpaque(true);
		setBackground(panelBackground);
		
		constraints.fill= GridBagConstraints.BOTH;
		constraints.weightx= 1.0;
		JLabel label1= new JLabel("average payrate:",JLabel.RIGHT);
		gridbag.setConstraints(label1, constraints);
		add(label1);
		progressBar= new JProgressBar(0,progressLimit);
		progressBar.setValue(0);
		progressBar.setStringPainted(true);
		progressBar.setAlignmentX(Component.CENTER_ALIGNMENT);
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(progressBar, constraints);
		add(progressBar);
		
		JLabel label2= new JLabel("game status:",JLabel.RIGHT);
		constraints.gridwidth= GridBagConstraints.RELATIVE;
		gridbag.setConstraints(label2, constraints);
		add(label2);
		messageBar= new JLabel("persona born",JLabel.CENTER);
		messageBar.setAlignmentX(Component.CENTER_ALIGNMENT);
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(messageBar, constraints);
		add(messageBar);
		
		JLabel label3= new JLabel("elapsed time:",JLabel.RIGHT);
		constraints.gridwidth= GridBagConstraints.RELATIVE;
		gridbag.setConstraints(label3, constraints);
		add(label3);
		timeBar= new JProgressBar(0, timeLimit*1000);
		timeBar.setValue(0);
		timeBar.setStringPainted(true);
		timeBar.setAlignmentX(Component.CENTER_ALIGNMENT);
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(timeBar, constraints);
		add(timeBar);
		
		JLabel label4= new JLabel("accumulated pay:",JLabel.RIGHT);
		constraints.gridwidth= GridBagConstraints.RELATIVE;
		gridbag.setConstraints(label4, constraints);
		add(label4);
//		winIcon= new PatchIcon(420,11);		// used for highlighting sections
//		dynamicLabel = new JLabel(winIcon);
		dynamicLabel = new JLabel();
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(dynamicLabel, constraints);
		add(dynamicLabel);
		
		JLabel label5= new JLabel("pay rate for:",JLabel.RIGHT);
//		label5.setBackground(panelBackground);
		constraints.gridwidth= GridBagConstraints.RELATIVE;
		gridbag.setConstraints(label5, constraints);
		add(label5);
		JPanel payDefn= new JPanel(new FlowLayout());
		payDefn.setOpaque(false);
//		payDefn.setBackground(panelBackground);
		label5_0= new JLabel(" fighting Kings:$0 ",JLabel.CENTER);
		label5_0.setOpaque(true);
//		label5_0.setBackground(Color.red);
		payDefn.add(label5_0);
		payDefn.add(new JLabel("   "));
		String pStr;
		String tipNote= tippy?"+tips ":" ";
		pStr= String.format(" Pawn:$%.2f/min"+tipNote, MarketISGame.pawnPayRate);
		label5_1= new JLabel(pStr,JLabel.CENTER);
		label5_1.setOpaque(true);
//		label5_1.setBackground(Color.yellow);
		payDefn.add(label5_1);
		payDefn.add(new JLabel("   "));
		tipNote= tippy?"-tips ":" ";
		pStr= String.format(" solo King:$%.2f/min"+tipNote,MarketISGame.kingPayRate);
		label5_2= new JLabel(pStr,JLabel.CENTER);
		label5_2.setOpaque(true);
//		label5_2.setBackground(Color.orange);
		payDefn.add(label5_2);
		constraints.gridwidth= GridBagConstraints.REMAINDER; //end row
		gridbag.setConstraints(payDefn, constraints);
		add(payDefn);
		}
	
	private float payRate=0, cumulativePay=0;
	
	/** Call this to update the values being displayed.
		@param pay the current rate at which the player is making money
		@param cum the total money accumulated already
		*/
	public void setPay(float pay, float cum) { 
//		winIcon.setValues(payRate,cumulativePay); 
		if (payRate!=pay) System.err.println(" changed payRate "+pay); 
		payRate= pay; cumulativePay= cum; 
		dynamicLabel.setText(String.format("  $%.2f",cumulativePay));
		dynamicLabel.repaint(); 
		}
	
	/** Call this to update the color-coded indicator of conflict status.
		Gutted at last minute. TODO: remove?
		*/
	public void setWinStatus(int status) { 
//		System.err.println(graphlet.eigenNode+" setWinStatus "+status);
		label5_0.setBackground(panelBackground);
		label5_1.setBackground(panelBackground);
		label5_2.setBackground(panelBackground);
/*		switch(status) {
			case 0: 
				label5_0.setBackground(Color.red);
				break;
			case 1: 
				label5_1.setBackground(Color.yellow);
				break;
			case 2: 
				label5_2.setBackground(Color.orange);
				break;
			}           ripped out at last minute  */
//		System.err.print((status==0)?".":(""+status));
		}
		
	/** A little icon class that displays the current payment values.
		*/
	class PatchIcon implements Icon,java.io.Serializable {
		private int wide, nPatches, patch, strhigh, status;
		public int getIconWidth() { return wide; }
		public int getIconHeight() { return 18; }
		public PatchIcon(int wd, int fontHigh) {
			wide=wd; strhigh= (fontHigh+getIconHeight())/2;
			}
		
		public void paintIcon(Component c, Graphics g, int x, int y) {
			//int horz= (int)(wide*payRate/2.5);
			//System.err.println("paintIcon "+horz+","+ y);
			//g.setColor(Color.GREEN);
			//g.fill3DRect(x, y, horz, getIconHeight(), true);
			//g.setColor(Color.BLACK);
			String data= String.format("  $%4.2f ", cumulativePay);
			int strwide= g.getFontMetrics().stringWidth(data);
			g.drawString(data,5,y+strhigh);
			}
		} // EOC PatchIcon
	
	}
